package com.jshcbd.modules.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshcbd.modules.asset.dto.ReceiveDto;
import com.jshcbd.modules.asset.entity.CompanyEntity;
import com.jshcbd.modules.asset.entity.ReceiveEntity;
import com.jshcbd.modules.asset.entity.ReceiveItemEntity;
import com.jshcbd.modules.asset.mapper.CompanyMapper;
import com.jshcbd.modules.asset.mapper.ReceiveMapper;
import com.jshcbd.modules.asset.service.IAssetService;
import com.jshcbd.modules.asset.vo.AssetVo;
import com.jshcbd.modules.asset.vo.ChartVo;
import com.jshcbd.modules.asset.entity.AssetEntity;
import com.jshcbd.modules.asset.mapper.AssetMapper;
import com.jshcbd.modules.asset.mapper.ReceiveItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Service
@Transactional
public class AssetServiceImpl extends ServiceImpl<AssetMapper, AssetEntity> implements IAssetService {

    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private ReceiveMapper receiveMapper;

    @Autowired
    private ReceiveItemMapper receiveItemMapper;

    @Autowired
    private CompanyMapper companyMapper;



    @Override
    public void doReceive(ReceiveDto receive) throws Exception {
        CompanyEntity companyEntity  = companyMapper.selectById(receive.getCompanyId());
        BigDecimal sumPrice = receiveMapper.querySumPrice(receive.getCompanyId());
        if(sumPrice == null){
            sumPrice = BigDecimal.ZERO;
        }
        BigDecimal temp =  sumPrice.add(receive.getSumPrice());

        if(temp.compareTo(companyEntity.getReceiveLimit()) == 1){
            throw new Exception("公司累计已领用："+sumPrice+",当前领用额度为："+receive.getSumPrice()+",超出公司领用额度，请检查！");
        }
        ReceiveEntity entity = new ReceiveEntity(receive);
        receiveMapper.insert(entity);
        Arrays.stream(receive.getAssetIds()).forEach(id ->{
            ReceiveItemEntity itemEntity = new ReceiveItemEntity().setAssetId(id).setMainId(entity.getId());
            receiveItemMapper.insert(itemEntity);
            //更新资产状态 领用
            assetMapper.updateAsset2Receive(id,entity.getId(),entity.getLocationBuilding(),entity.getLocationFloor());
        });

        log.info(String.format("成功更新条资产领取%s条数据，资产领取记录Id：%s",receive.getAssetIds().length,entity.getId()));

    }

    @Override
    public IPage<AssetVo> pageView(Page<AssetVo> page, Wrapper wrapper) {
        return assetMapper.pageView(page,wrapper);
    }

    @Override
    public List<AssetVo> listView(Wrapper wrapper) {
        return assetMapper.listView(wrapper);
    }

    @Override
    public List<ChartVo> pieByState() {
        return assetMapper.queryPieByState();
    }

    @Override
    public List<ChartVo> pieByType() {
        return assetMapper.queryPieByTypeName();
    }

    @Override
    public List<ChartVo> pieByClassess() {
        return assetMapper.queryPieByClasses();
    }

    @Override
    public List<ChartVo> groupByInstoreDate(Integer year) {
        return assetMapper.queryByInstoreDate(year);
    }


}
